import { LibAppView } from "@/lib";
import React, { useCallback, useEffect, useMemo, useRef } from "react";
import { useDelete } from "../../hooks/useDelete";
import { FormattedMessage } from "react-intl";
import { Button, Tag } from "antd";
import { ResourceList } from "../../Component/ResourceList";
import { PlusOutlined } from "@ant-design/icons";
import { AdminRight } from "@/pages/Admin/data/AdminPageAuth";


const CarourseImage = LibAppView.withService({
    reqEndPoints: [{
        endPoint: "carousel.category",
        name: "category"
    }]
})((props) => {
    const categoryList = useMemo(() => {
        const categoryData = ((props.responseData as any).category?.data || []).map((str: string) => ({
            label: str,
            value: str
        }));
        return categoryData;
    }, [props.responseData]);
    const listRef = useRef<any>();
    const microApp = LibAppView.useMicroApp();
    const imgUrl = props.withServiceApi.getUrl("resource.mediaUrl");
    const deleteAction = useDelete({
        endPoint: "carousel.delete",
        cancelEndPoint: "carousel.submit",
        data: {},
        listRef
    });
    const columns = useMemo(() => ([
        {
            title: "模块",
            dataIndex: "module",
            width: 60,
            fixed: true
        },
        {
            title: <FormattedMessage id="admin.pages.article.title" />,
            dataIndex: "title",
            fixed: true
        },{
            title: "图片",
            dataIndex: "image",
            width: 100,
            render: (image: string,data: any) => {
                const imgUrlLink = imgUrl + "/" + image;
                return image?.length > 0 ? <img style={{maxWidth: 100, maxHeight: 100}} src={imgUrlLink} alt={data.title} /> : <></>
            }
        },
        {
            title: <FormattedMessage id="admin.pages.menus.edit.status"/>,
            dataIndex: "status",
            width: 100,
            render: (status: number) => {
                return (<>
                    { status === 1 && <Tag color="green"><FormattedMessage id="admin.pages.article.edit.status.enable"/></Tag> }
                    { status === 0 && <Tag color="error"><FormattedMessage id="admin.pages.article.edit.status.disable"/></Tag> }
                </>);
            }
        }, {
            title: <FormattedMessage id="admin.pages.menus.list.sort"/>,
            dataIndex: "sort_id",
            width: 100
        },{
            title: <FormattedMessage id="admin.pages.language.operate"/>,
            dataIndex: "id",
            width: 140,
            render: (id: number, data: any) => {
                return (<>
                    <AdminRight authRight="editable" mode="disabled">
                        <Button size="small" onClick={()=>{
                            microApp.startJourney("imageEditorJourney", {
                                state: data
                            });
                        }}>
                            <FormattedMessage id="admin.pages.operate.edit"/>
                        </Button>
                    </AdminRight> 
                    <AdminRight authRight="deleteable" mode="disabled">
                        <Button size="small" onClick={() => {
                            deleteAction.delete({
                                ...data,
                                status: -1
                            })
                        }}><FormattedMessage id="admin.pages.operate.delete"/></Button>
                    </AdminRight>
                </>);
            }
        }
    ]), [deleteAction]);
    const SearchFields: Admin.IAdvancedField[] = useMemo(() =>([
        {
            type: "Select",
            label: "admin.pages.article.category",
            name: "categoryName",
            placeholder: "admin.layout.input.placeholder",
            props: { autoComplete: "off" },
            data: [{
                label: <FormattedMessage id="admin.pages.article.allCategory" />,
                value: null
            }, ...(categoryList || [])]
        },{
            type: "Input",
            label: "admin.pages.article.title",
            name: "title",
            placeholder: "admin.layout.input.placeholder",
        }
    ]), [categoryList]);
    const listEndPoint = useMemo(() => ({
        endPoint: "carousel.list",
    }), []);
    useEffect(() => {
        return microApp.registeJourney({
            name: "imageEditorJourney",
            title: "test journey",
            stepJourney: true,
            isTempJourney: true,
            steps: [{
                isMemoryPage: true,
                page: "admin.carouselImageEdit"
            }]
        });
    }, []);
    const onAddClick = useCallback(() => {
        microApp.startJourney("imageEditorJourney");
    }, []);
    const Operator = useMemo(() => {
        return () => {
            return (<AdminRight authRight="editable" mode="disabled">
                <Button type="primary" onClick={onAddClick}>
                    <PlusOutlined />
                    <span><FormattedMessage id="admin.pages.operate.add" /></span>
                </Button>
            </AdminRight>);
        };
    }, [onAddClick]);
    console.log(categoryList);
    return (<ResourceList
        refApi={listRef}
        tableColumns={columns}
        searchFields={SearchFields} 
        listEndPoint={listEndPoint}
        tableWidth="auto"
        Operators={Operator}
    />);
});

export default CarourseImage;